package HP.com.book.wangyu.dao.impl;


import HP.com.book.wangyu.dao.UserDao;
import HP.com.book.wangyu.pojo.User;
import HP.p0829.oop5.UDisk;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Collection;
import java.util.Scanner;

/**
 * 用户实现
 */
public class UserDaoImpl implements UserDao {
    private final String DRIVER_CLASS = "oracle.jdbc.driver.OracleDriver";
    private final String URL = "jdbc:oracle:thin:localhost:1521:orcl";
    private final String UNAME = "system";
    private final String UPWD = "123456";

    /**
     * 输入字符串
     *
     * @return
     */
    @Override
    public String inPut() {
        Scanner sc = new Scanner(System.in);
        return sc.nextLine();
    }

    /**
     * 验证登录
     *
     * @param name
     * @param pwd
     * @return
     */
    @Override //登录
    public User login(String name, String pwd) {
        User user = this.selectUserInfo(name);
        if (user.getUserName() == null) {
            return null;
        } else if (user.getUserPwd().equals(pwd)) {
            return user;
        } else {
            return null;
        }
    }

    /**
     * 查询用户信息
     *
     * @param userName
     * @return
     */
    @Override //查询用户信息
    public User selectUserInfo(String userName) {
        User userInfo = new User();
        try {
            String sql = "select * from BM_USER where username=?";
            PreparedStatement pre = this.getConn().prepareStatement(sql);
            pre.setString(1, userName);
            ResultSet res = pre.executeQuery();
            while (res.next()) {
                userInfo.setUserId(res.getInt("userid"));
                userInfo.setUserName(res.getString("username"));
                userInfo.setUserPwd(res.getString("userpwd"));
            }
            res.close();
            pre.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return userInfo;
    }

    @Override
    public Connection getConn() {
        try {
            Class.forName(DRIVER_CLASS);
            Connection conn = DriverManager.getConnection(URL, UNAME, UPWD);
            return conn;
        } catch (Exception e) {
            e.getMessage();
        }
        return null;
    }
}
